<template>
	<view class="mine-container">
		<view class="user-info-section" @click="handleUserInfoClick">
			<view class="avatar-container">
				<image class="avatar" src="../../static/icons/mine.png"></image>

			</view> 
			<view class="user-details">
				<text class="username">{{ isLogin ? userInfo.phone : '点击登录' }}</text>
				<text class="user-desc">{{ isLogin ? '已登录' : '登录后查看您的个人信息' }}</text>
			</view>
			<view v-if="isLogin" class="logout-btn" @click.stop="handleLogout">
				<text class="logout-text">退出</text>
			</view>
		</view>
		<!-- <view class="function-section">
			<view class="function-item" @click="navigateTo('orders')">
				<view class="function-icon-container">
					<image class="function-icon" src="/static/icons/order.png"></image>
				</view>
				<text class="function-text">我的订单</text>
				<uni-icons type="right" size="16" color="#999"></uni-icons>
			</view>
			
			<view class="function-item" @click="navigateTo('registration')">
				<view class="function-icon-container">
					<image class="function-icon" src="/static/icons/registration.png"></image>
				</view>
				<text class="function-text">我的报名信息</text>
				<uni-icons type="right" size="16" color="#999"></uni-icons>
			</view>
			
			<view class="function-item" @click="navigateTo('invoice')">
				<view class="function-icon-container">
					<image class="function-icon" src="/static/icons/invoice.png"></image>
				</view>
				<text class="function-text">发票信息</text>
				<uni-icons type="right" size="16" color="#999"></uni-icons>
			</view>
		</view> -->

		<view class="other-section">
			
			<view class="function-item" @click="navigateTo('help')">
				<view class="function-icon-container">
					<image class="function-icon" src="/static/icons/help.png"></image>
				</view>
				<text class="function-text">联系我们</text>
				<uni-icons type="right" size="16" color="#999"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			isLogin: false,
			userInfo: {
				id: '',
				phone: ''
			},
			API_CONFIG: {
				BASE_URL: "https://www.xn--fiq799a58u.xn--fiqs8s"
			}
		}
	},
	onShow() {
		this.checkLoginStatus();
	},
	methods: {
		checkLoginStatus() {
			const token = uni.getStorageSync('token');
			const userPhone = uni.getStorageSync('userPhone');
			const userId = uni.getStorageSync('userId');
			
			if (token && userPhone && userId) {
				this.isLogin = true;
				this.userInfo.id = userId;
				this.userInfo.phone = this.formatPhone(userPhone);
			} else {
				this.isLogin = false;
				this.userInfo = {
					id: '',
					phone: ''
				};
				// 清除可能残留的无效 token
				if (token) {
					uni.removeStorageSync('token');
				}
			}
		},
		
		formatPhone(phone) {
			if (!phone || phone.length !== 11) return phone;
			return phone.substring(0, 3) + '****' + phone.substring(7);
		},
		
		getUserInfo(token) {
			try {
				if (!token) {
					console.warn('未提供有效的 token');
					return;
				}
				uni.request({
					url: `${this.API_CONFIG.BASE_URL}/api/current/userInfo`,
					method: 'GET',
					header: {
						'Authorization': `Bearer ${token}`
					},
					success: (res) => {
						if (res.statusCode === 200) {
							try {
								const userInfo = res.data;
								if (userInfo && userInfo.id) {
									uni.setStorageSync('userId', userInfo.id);
									this.userInfo.id = userInfo.id;
								}
							} catch (e) {
								console.error('解析用户信息失败:', e);
							}
						} else if (res.statusCode === 403) {
							// 处理 token 失效的情况
							uni.removeStorageSync('token');
							this.isLogin = false;
							this.userInfo = { id: '', phone: '' };
							uni.showToast({
								title: '登录已过期，请重新登录',
								icon: 'none'
							});
						}
					},
					fail: () => {
						console.warn('获取用户信息请求失败');
					}
				});
			} catch (error) {
				console.error('获取用户信息过程出错:', error);
			}
		},
		
		handleUserInfoClick() {
			if (!this.isLogin) {
				uni.navigateTo({
					url: '/pages/login/index'
				});
			} else {
				this.getUserInfo(uni.getStorageSync('token'));
			}
		},
		
		handleLogout() {
			uni.showModal({
				title: '提示',
				content: '确定要退出登录吗？',
				success: (res) => {
					if (res.confirm) {
						// 清除登录信息
						uni.removeStorageSync('token');
						uni.removeStorageSync('userPhone');
						uni.removeStorageSync('userId');
						
						// 更新状态
						this.isLogin = false;
						this.userInfo = {
							id: '',
							phone: ''
						};
						
						uni.showToast({
							title: '已退出登录',
							icon: 'success'
						});
					}
				}
			});
		},
		
		navigateTo(page) {
			if (!this.isLogin && page !== 'help') {
				uni.showModal({
					title: '提示',
					content: '请先登录',
					success: (res) => {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/index'
							});
						}
					}
				});
				return;
			}
			
			// 跳转到对应页面
			uni.navigateTo({
				url: `/pages/${page}/index`
			});
		}
	}
}
</script>

<style>
.mine-container {
	display: flex;
	flex-direction: column;
	min-height: 100vh;
	background-color: #f5f5f5;
}

/* 用户信息区域样式 */
.user-info-section {
	display: flex;
	align-items: center;
	padding: 40rpx 30rpx;
	background-color: #44464b;
	color: #fff;
}

.avatar-container {
	margin-right: 30rpx;
}

.avatar {
	width: 120rpx;
	height: 120rpx;
	border-radius: 50%;
	border: 4rpx solid rgba(255, 255, 255, 0.5);
}

.user-details {
	flex: 1;
}

.username {
	font-size: 36rpx;
	font-weight: bold;
	margin-bottom: 10rpx;
	font-weight: bold;
	display: block;
}

.user-desc {
	font-size: 26rpx;
	opacity: 0.8;
}

/* 退出按钮样式 */
.logout-btn {
	position: absolute;
	right: 30rpx;
	padding: 10rpx 20rpx;
	background-color: rgba(255, 255, 255, 0.2);
	border-radius: 30rpx;
}

.logout-text {
	font-size: 24rpx;
	color: #fff;
}

/* 功能按钮区域样式 */
.function-section, .other-section {
	margin-top: 20rpx;
	background-color: #fff;
	border-radius: 10rpx;
	padding: 0 20rpx;
}

.section-title {
	font-size: 28rpx;
	color: #666;
	padding: 20rpx 10rpx;
	border-bottom: 1rpx solid #f0f0f0;
}

.function-item {
	display: flex;
	align-items: center;
	padding: 30rpx 10rpx;
	border-bottom: 1rpx solid #f0f0f0;
}

.function-item:last-child {
	border-bottom: none;
}

.function-icon-container {
	width: 50rpx;
	height: 50rpx;
	margin-right: 20rpx;
	display: flex;
	justify-content: center;
	align-items: center;
}

.function-icon {
	width: 40rpx;
	height: 40rpx;
}

.function-text {
	flex: 1;
	font-size: 28rpx;
	color: #333;
}
</style>
